package cn.iocoder.yudao.module.handguard.controller.admin.productaccesspercentage;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.admin.productaccesspercentage.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.productaccesspercentage.ProductAccessPercentageDO;
import cn.iocoder.yudao.module.handguard.convert.productaccesspercentage.ProductAccessPercentageConvert;
import cn.iocoder.yudao.module.handguard.service.productaccesspercentage.ProductAccessPercentageService;

@Tag(name = "管理后台 - 准入产品-提成参数")
@RestController
@RequestMapping("/hutai/product-access-percentage")
@Validated
public class ProductAccessPercentageController {

    @Resource
    private ProductAccessPercentageService productAccessPercentageService;




    @GetMapping("/getByOrderId")
    @Operation(summary = "获得准入产品-提成参数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ProductAccessPercentageRespVO> getProductAccessPercentageByOrderId(@RequestParam("id") Long id) {
        ProductAccessPercentageDO productAccessPercentage = productAccessPercentageService.getProductAccessPercentageByOrderId(id);
        return success(ProductAccessPercentageConvert.INSTANCE.convert(productAccessPercentage));
    }

    @PostMapping("/saveOrUpdate")
    @Operation(summary = "新增或修改-提成参数")
    public CommonResult<Boolean> saveOrUpdateProductAccessPercentage(@Valid @RequestBody ProductAccessPercentageUpdateReqVO updateReqVO) {
        productAccessPercentageService.saveOrUpdateProductAccessPercentage(updateReqVO);
        return success(true);
    }



    @PostMapping("/create")
    @Operation(summary = "创建准入产品-提成参数")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-percentage:create')")
    public CommonResult<Long> createProductAccessPercentage(@Valid @RequestBody ProductAccessPercentageCreateReqVO createReqVO) {
        return success(productAccessPercentageService.createProductAccessPercentage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新准入产品-提成参数")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-percentage:update')")
    public CommonResult<Boolean> updateProductAccessPercentage(@Valid @RequestBody ProductAccessPercentageUpdateReqVO updateReqVO) {
        productAccessPercentageService.updateProductAccessPercentage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除准入产品-提成参数")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:product-access-percentage:delete')")
    public CommonResult<Boolean> deleteProductAccessPercentage(@RequestParam("id") Long id) {
        productAccessPercentageService.deleteProductAccessPercentage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得准入产品-提成参数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-percentage:query')")
    public CommonResult<ProductAccessPercentageRespVO> getProductAccessPercentage(@RequestParam("id") Long id) {
        ProductAccessPercentageDO productAccessPercentage = productAccessPercentageService.getProductAccessPercentage(id);
        return success(ProductAccessPercentageConvert.INSTANCE.convert(productAccessPercentage));
    }


    @GetMapping("/list")
    @Operation(summary = "获得准入产品-提成参数列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-percentage:query')")
    public CommonResult<List<ProductAccessPercentageRespVO>> getProductAccessPercentageList(@RequestParam("ids") Collection<Long> ids) {
        List<ProductAccessPercentageDO> list = productAccessPercentageService.getProductAccessPercentageList(ids);
        return success(ProductAccessPercentageConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得准入产品-提成参数分页")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-percentage:query')")
    public CommonResult<PageResult<ProductAccessPercentageRespVO>> getProductAccessPercentagePage(@Valid ProductAccessPercentagePageReqVO pageVO) {
        PageResult<ProductAccessPercentageDO> pageResult = productAccessPercentageService.getProductAccessPercentagePage(pageVO);
        return success(ProductAccessPercentageConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出准入产品-提成参数 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:product-access-percentage:export')")
    @OperateLog(type = EXPORT)
    public void exportProductAccessPercentageExcel(@Valid ProductAccessPercentageExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProductAccessPercentageDO> list = productAccessPercentageService.getProductAccessPercentageList(exportReqVO);
        // 导出 Excel
        List<ProductAccessPercentageExcelVO> datas = ProductAccessPercentageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "准入产品-提成参数.xls", "数据", ProductAccessPercentageExcelVO.class, datas);
    }

}
